package qq.android.wan.qq.adapter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Hashtable;
import java.util.Set;

import android.content.Context;
import android.provider.Telephony;
import android.text.Spannable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMConversation;
import com.easemob.chat.EMMessage;
import com.easemob.chat.FileMessageBody;
import com.easemob.chat.ImageMessageBody;
import com.easemob.chat.LocationMessageBody;
import com.easemob.chat.MessageBody;
import com.easemob.chat.TextMessageBody;
import com.easemob.chat.VideoMessageBody;
import com.easemob.chat.VoiceMessageBody;

import qq.android.wan.qq.R;
import qq.android.wan.qq.chat.utils.SmileUtils;
import qq.android.wan.qq.entity.FriendBean;
import qq.android.wan.qq.entity.SessionBean;

public class ItemLvHistoryAdapter extends BaseAdapter {

    private ArrayList<EMConversation> objects;

    private Context context;
    private LayoutInflater layoutInflater;

    public ItemLvHistoryAdapter(Context context, ArrayList<EMConversation> objects) {
        this.context = context;
        this.objects = objects;
        this.layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return objects == null ? 0 : objects.size();
    }

    @Override
    public EMConversation getItem(int position) {
        return objects.get(position);

    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;

        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = layoutInflater.inflate(R.layout.adapter_session_fragment_tab1, null);
            viewHolder.tv_lastMessage = (TextView) convertView.findViewById(R.id.tv_lastMessage);
            viewHolder.tv_lastTime = (TextView) convertView.findViewById(R.id.tv_lastTime);
            viewHolder.tv_nick = (TextView) convertView.findViewById(R.id.tv_nick);
            viewHolder.iv_head = (ImageView) convertView.findViewById(R.id.iv_head);
            viewHolder.tv_unread_num = (TextView) convertView.findViewById(R.id.tv_unread_num);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        EMConversation conversation = objects.get(position);
        EMMessage message = conversation.getLastMessage();
        viewHolder.iv_head.setImageResource(R.drawable.ziji);

        viewHolder.tv_lastTime.setText(showTime(message.getMsgTime()));

        viewHolder.tv_lastMessage.setText(getLastMessageContent(message),TextView.BufferType.SPANNABLE);
        // EMConversation conversation = EMChatManager.getInstance().getConversationByType(sessionBean.getUserId()+"", EMConversation.EMConversationType.Chat);
        int unread_num = conversation.getUnreadMsgCount();
        if (unread_num != 0) {
            viewHolder.tv_unread_num.setVisibility(View.VISIBLE);
            viewHolder.tv_unread_num.setText(unread_num + "");
        } else {
            viewHolder.tv_unread_num.setVisibility(View.INVISIBLE);
        }
        viewHolder.tv_nick.setText(conversation.getUserName());
        return convertView;
    }


    protected class ViewHolder {
        private TextView tv_lastMessage, tv_lastTime, tv_nick, tv_unread_num;
        private ImageView iv_head;
    }
private CharSequence getLastMessageContent(EMMessage message){
    CharSequence str="";
    MessageBody messageBody=message.getBody();
    if(messageBody instanceof TextMessageBody){
        Spannable span = SmileUtils.getSmiledText(context, ((TextMessageBody)messageBody).getMessage());
        str=span;
    }else if(messageBody instanceof VideoMessageBody){
        str="[视频]";
    }else if(messageBody instanceof ImageMessageBody){
        str="[图片]";
    }else if(messageBody instanceof VoiceMessageBody){
        str="[语音]";
    }else if(messageBody instanceof LocationMessageBody){
        str="[位置]";
    }else if(messageBody instanceof FileMessageBody){
        str="[文件]";
    }
    return str;
}
    /**
     * 将"2016-04-02 17:48:01"时间转化
     *
     * @param str_time
     * @return
     */
    private String showTime(long str_time) {
        String result = "";
        Log.e("test", "时间:" + str_time);

            /*SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date nowDate = simpleDateFormat.*/
        Calendar chatCalendar = Calendar.getInstance();
        chatCalendar.setTimeInMillis(str_time);
        Calendar nowCalendar = Calendar.getInstance();
        //1）首先判断是今天
        if (nowCalendar.get(Calendar.YEAR) == chatCalendar.get(Calendar.YEAR) && nowCalendar.get(Calendar.DAY_OF_YEAR) == chatCalendar.get(Calendar.DAY_OF_YEAR)) {
            //显示的是上午和下午
            if (chatCalendar.get(Calendar.HOUR_OF_DAY) > 12) {
                //下午
                result = "下午" + (chatCalendar.get(Calendar.HOUR_OF_DAY) - 12) + ":" + chatCalendar.get(Calendar.MINUTE);
            } else {
                result = "上午" + (chatCalendar.get(Calendar.HOUR_OF_DAY)) + ":" + chatCalendar.get(Calendar.MINUTE);
            }
        } else {
            // 2）判断是不是昨天
            if (chatCalendar.get(Calendar.YEAR) == nowCalendar.get(Calendar.YEAR) && chatCalendar.get(Calendar.DAY_OF_YEAR) + 1 == nowCalendar.get(Calendar.DAY_OF_YEAR)) {
                result = "昨天";
            } else {
                result = dayToWeek(chatCalendar.get(Calendar.DAY_OF_WEEK));
            }
        }

        return result;
    }

    private String dayToWeek(int a) {
        String result = "星期";
        String num = "";
        switch (a) {
            case 1:
                num = "日";
                break;
            case 2:
                num = "一";
                break;
            case 3:
                num = "二";
                break;
            case 4:
                num = "三";
                break;
            case 5:
                num = "四";
                break;
            case 6:
                num = "五";
                break;
            case 7:
                num = "六";
                break;


        }
        return result + num;
    }
}
